﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using YuShiManageSystem.DATA;
using YuShiManageSystem.COM;

namespace YuShiManageSystem.UI.Card
{
    /// <summary>
    /// 会员卡充值窗体
    /// </summary>
    public partial class AddMoney : YuShiManageSystem.UI.Base
    {
        private tb_card card = null;
        public AddMoney()
        {
            InitializeComponent();
        }
        public AddMoney(Session se)
            : base(se)
        {
            InitializeComponent();
            this.cmbPayMode.Items.AddRange(Session.PayType);
        }
        
        /// <summary>
        /// 刷卡事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtCardId_KeyPress(object sender, KeyPressEventArgs e)
        {
            this.card = new CardService(Session.DataModel).GetCardByCode(txtCode.Text.Trim());
            if (card == null)
            {
                YSMessage.Show("VIP卡没找到");
                return;
            }
            this.txtAmount.Text = card.useAmount.ToString();
            this.txtDebtAmount.Text = card.debt.ToString();
            this.txtSubCode.Focus();

        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            //验证

            decimal debt = (decimal)this.card.debt;
            
            //修改卡里可用余额+=补款额，补款额清零，
            this.card.useAmount += this.card.debt;
            this.card.debt = 0;
            
            //写消费单
            tb_order order = new tb_order();
            //order.orderNo
            order.storeId = this.Session.Store.storeId;
            order.cardId = card.cardId;
            //order.cardType = card.tb_cardType.typeName;
            order.subCode = txtSubCode.Text.Trim();
            order.createDate = DateTime.Now.Date;
            order.remark = txtRemark.Text.Trim();

            //写员工业绩
            List<tb_business> business = null;
            foreach (DataGridViewRow r in this.dgvEmployee.Rows)
            {
                if (r.IsNewRow) continue;
                tb_business b = new tb_business();
                b.personNo = r.Cells[0].Value.ToString();
                b.createDate = DateTime.Now.Date;
                b.category = Session.RESULT_CASH;    
                b.complete = Convert.ToDecimal(r.Cells[1].Value);
                b.amount = Convert.ToDecimal(r.Cells[2].Value);
                business.Add(b);
            }
            //写卡异动
            tb_maintenance maintenance = new tb_maintenance();
            maintenance.cardId = card.cardId;
            maintenance.changeDate = DateTime.Now.Date;
            maintenance.changeType = Session.CARD_OPEN;
            maintenance.amount = debt;

            //保存
            List<System.Data.Objects.DataClasses.EntityObject> entities = new List<System.Data.Objects.DataClasses.EntityObject>();
            entities.Add(this.card);
            entities.Add(order);
            entities.AddRange(business);
            entities.Add(maintenance);
            if (this.Session.DataModel.Save(entities) > 0)
                YSMessage.Show("补款成功！");
            else
                YSMessage.Show("操作失败，重试！");
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
       
    }
}
